<?php
class AccountReceivableSearch extends CFormModel {
	public $receiveDateMin;
	public $receiveDateMax;
	public $keyword;
	public $priceMin;
	public $priceMax;
	public $projectId;

	public function rules() {
		return array(
			array(
				'projectId',
				'numerical',
				'integerOnly' => true,
			),
			array(
				'priceMin, priceMax',
				'numerical',
			),
			array(
				'receiveDateMin, receiveDateMax',
				'date',
				'format' => 'yyyy-MM-dd',
			),
			array(
				'keyword',
				'length',
				'max' => 255
			),
		);
	}

	public function attributeLabels() {
		return array(
			'priceMin' => '收款金额',
			'keyword' => '关键词',
			'projectId' => '项目',
			'receiveDateMin' => '收款时间'
		);
	}

	/**
	 * @return CDbCriteria
	 */
	public function createCriteria($tableAlias, $fast = false) {
		$criteria = new CDbCriteria;
		if (!$fast) {
			$criteria->with = array(
				//'merchant',
				'project'
			);
		}

		if ($this->receiveDateMin) {
			$criteria->compare('receiveDate', '>=' . $this->receiveDateMin);
		}
		if ($this->receiveDateMax) {
			$criteria->compare('receiveDate', '<=' . $this->receiveDateMax);
		}
		if ($this->priceMin) {
			$criteria->compare('price', '>=' . $this->priceMin);
		}
		if ($this->priceMax) {
			$criteria->compare('price', '<=' . $this->priceMax);
		}

		$criteria->compare('projectId', $this->projectId);
		$criteria->compare($tableAlias.'.note', $this->keyword, true);

		$criteria->order = 'receiveDate DESC';

		return $criteria;
	}
}
